package cn.kgc.ubagnet.service.buyer;

import cn.kgc.ubagnet.mapper.BuyerMapper;
import cn.kgc.ubagnet.pojo.Buyer;
import cn.kgc.ubagnet.pojo.BuyerExample;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-08-04 11:33
 */
@Service
public class BuyerServiceImpl implements BuyerService {


    @Resource
    BuyerMapper buyerMapper;
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public Buyer login(String phone, String password) {
        BuyerExample example = new BuyerExample();
        BuyerExample.Criteria criteria = example.createCriteria();
        criteria.andPhoneEqualTo(phone);
        List<Buyer> list = buyerMapper.selectByExample(example);
        if(list!=null&&list.size()>0){
            Buyer admin=list.get(0);
            if(admin==null){
                return null;
            }
            if(admin.getPassword().equals(password)){
                return admin;
            }
        }
        return null;
    }

    @Override
    public Integer addBuyer(Buyer buyer) {
        String password = buyer.getPassword();
//        userPswd = CrowdFundingUtils.md5(userPswd);
        String encode = bCryptPasswordEncoder.encode(password);
        buyer.setPassword(encode);
        return   buyerMapper.insert(buyer);
    }
}
